package com.qinsong.myapplication.controller.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.qinsong.myapplication.R;
import com.qinsong.myapplication.controller.adapter.InvitationAdapter;
import com.qinsong.myapplication.model.Model;
import com.qinsong.myapplication.model.bean.InvitationInfo;
import com.qinsong.myapplication.utils.Constant;

import java.util.List;

public class InvitationActivity extends Activity {

    private BroadcastReceiver InvitationChangeReciver =  new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            refresh();
        }
    };
    private ListView invitation;
    InvitationAdapter.OnClickListener mOnClickListener = new InvitationAdapter.OnClickListener() {
        @Override
        public void Accept(final InvitationInfo invitationInfo) {
            //告知环信服务器已经点击接受
            Model.getInstance().getGlobaThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        EMClient.getInstance().contactManager().acceptInvitation(invitationInfo.getUserInfo().getHxid());
                        //数据库更新
                        Model.getInstance().getDbManager().getInvitationDao().updateInvitationStatus(InvitationInfo.InvitationStatus.INVITE_ACCEPT,invitationInfo.getUserInfo().getHxid());
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //页面改变
                                Toast.makeText(InvitationActivity.this,"接受了邀请",Toast.LENGTH_SHORT).show();
                                //刷新页面
                                refresh();
                            }


                        });


                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        runOnUiThread(new Runnable() {
                            //接受失败
                            @Override
                            public void run() {
                                //页面改变
                                Toast.makeText(InvitationActivity.this,"接受邀请失败",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
            });
        }

        @Override
        public void Reject(final InvitationInfo invitationInfo) {
            Model.getInstance().getGlobaThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        EMClient.getInstance().contactManager().declineInvitation(invitationInfo.getUserInfo().getHxid());
                        //数据库变化
                        Model.getInstance().getDbManager().getInvitationDao().removeInvitation(invitationInfo.getUserInfo().getHxid());

                        //页面变化
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InvitationActivity.this,"拒绝成了",Toast.LENGTH_SHORT).show();
                                refresh();
                            }
                        });
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        //页面变化
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(InvitationActivity.this,"拒绝失败",Toast.LENGTH_SHORT).show();
                                refresh();
                            }
                        });

                    }
                }
            });

        }
    };
    private InvitationAdapter invitationAdapter;
    private LocalBroadcastManager mLBM;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invitation);
        initView();
        initData();
    }

    private void initData() {
        //初始化listview
        invitationAdapter = new InvitationAdapter(this,mOnClickListener);
        invitation.setAdapter(invitationAdapter);
        refresh();
        //注册邀请信息变化广播
        mLBM = LocalBroadcastManager.getInstance(this);
        mLBM.registerReceiver(InvitationChangeReciver,new IntentFilter(Constant.CONTACT_INVITE_CHANGED));
    }

    /**
     *刷新
     */
    private void refresh() {
        //获取数据库中的所有邀请信息
        List<InvitationInfo> invitationInfos = Model.getInstance().getDbManager().getInvitationDao().getInvitations();
        //刷新（往adapter中添加数据）
        invitationAdapter.refresh(invitationInfos);
    }
    private void initView() {
        invitation = findViewById(R.id.invitation);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLBM.unregisterReceiver(InvitationChangeReciver);
    }
}
